﻿using System;                                                                                                                                       //MATT IS KING
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;

namespace Hired
{
    /*  A JobseekerAccount class used by jobseekers to log in. */
    public class DBJobseekerAccount
    {

        // add a Jobseeker Account
        public static Boolean addJobseekerAccount(JobseekerAccount ja, Jobseeker j)
        {
            try
            {
                ja.JAccountID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToJobseekerAccounts(ja);
                    ja.Jobseeker = j;
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static Boolean addJobsToWatchlist(JobseekerAccount ja, Job j)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(ja);
                    db.Attach(j);
                    ja.Jobs.Add(j);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool checkJobsWatchList(string accountID, string jobID)
        {
            Job jb = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                var query =
                    (from j in db.Jobs
                     .Where(j => j.JobID == jobID)
                     .Where(j => j.JobseekerAccounts.Any(a => a.JAccountID == accountID))
                     select j).SingleOrDefault();
                if (query == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }           
        }

        //retrieve a Jobseeker Account
        public static JobseekerAccount retrieveJobseekerAccount(string jAccountID, bool jobseeker)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                if (jobseeker)
                {
                    return
                        (from a in db.JobseekerAccounts.Include("Jobseeker")
                         where a.JAccountID == jAccountID
                         select a).SingleOrDefault();
                }
                else
                {
                    return
                        (from a in db.JobseekerAccounts
                         where a.JAccountID == jAccountID
                         select a).SingleOrDefault();
                }
            }
        }

        //retrieve a Jobseeker Username
        public static JobseekerAccount retrieveJobseekerAccountbyUsername(string username, bool jobseeker)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                if (jobseeker)
                {
                    return
                         (from a in db.JobseekerAccounts
                          where a.Username == username
                          select a).SingleOrDefault();
                }
                else
                {
                    return
                        (from a in db.JobseekerAccounts.Include("Jobseeker")
                         where a.Username == username
                         select a).SingleOrDefault();
                }
            }
        }

        public static JobseekerAccount RetrieveJobseekerAccountByEmail(string email)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return (JobseekerAccount)
                    (from ja in db.JobseekerAccounts
                     where ja.Email == email
                     select ja).SingleOrDefault();
            }
        }
        //retrieve a Jobseeker's jobs in watchlist
        public static List<Job> retrieveJobsByJobseekerAccountId(string jsaId)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return (
                    from a in db.JobseekerAccounts
                    from j in a.Jobs
                    where a.JAccountID == jsaId
                    select j).ToList<Job>();
            }
        }

        //update a Jobseeker Account
        public static Boolean updateJobseekerAccount(JobseekerAccount update)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobseekerAccount accountToUpdate = retrieveJobseekerAccount(update.JAccountID, false);
                    db.Attach(accountToUpdate);
                    accountToUpdate.Email = update.Email;
                    accountToUpdate.Username = update.Username;
                    accountToUpdate.SecurityQuestion = update.SecurityQuestion;
                    accountToUpdate.SecurityAnswer = update.SecurityAnswer;
                    accountToUpdate.PrivacySettings = update.PrivacySettings;
                    accountToUpdate.Password = update.Password;
                    accountToUpdate.AllowNotifications = update.AllowNotifications;
                    accountToUpdate.AllowMessages = update.AllowMessages;
                    accountToUpdate.Activated = update.Activated;

                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //delete a Jobseeker Account
        public static Boolean deleteJobseekerAccount(JobseekerAccount j)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobseekerAccount accountToDelete = retrieveJobseekerAccount(j.JAccountID, false);
                    db.Attach(accountToDelete);
                    db.DeleteObject(accountToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static Boolean deleteJobseekerAccountById(string id)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobseekerAccount accountToDelete = retrieveJobseekerAccount(id, false);
                    db.Attach(accountToDelete);
                    db.DeleteObject(accountToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        //search for Jobseeker Accounts
        public static List<JobseekerAccount> searchJobseekerAccount(string searchQuery)
        {
            searchQuery = searchQuery.ToUpper();
            using (hireddbEntities db = new hireddbEntities())
            {
                var searchResults =
                     from a in db.JobseekerAccounts.Include("Jobseeker")
                     where
                     a.Email.ToUpper().Contains(searchQuery)
                     || a.Username.ToUpper().Contains(searchQuery)
                     || a.Jobseeker.FirstName.ToUpper().Contains(searchQuery)
                     || a.Jobseeker.LastName.ToUpper().Contains(searchQuery)
                     || a.Jobseeker.Specialization.ToUpper().Contains(searchQuery)
                     || a.Jobseeker.WebsiteURL.ToUpper().Contains(searchQuery)
                     select a;
                return searchResults.ToList<JobseekerAccount>();
            }
        }
        public static List<JobseekerAccount> advancedSearchJobseekerAccount(List<JobseekerAccount> searchResults, string educationalInstitute, string exclude)
        {
            educationalInstitute = educationalInstitute.ToUpper();
            List<JobseekerAccount> advancedSearchResults = searchResults;
            //exclude
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                JobseekerAccount a = advancedSearchResults[i];
                if (a.Email.ToUpper().Contains(exclude)
                     || a.Username.ToUpper().Contains(exclude)
                     || a.Jobseeker.FirstName.ToUpper().Contains(exclude)
                     || a.Jobseeker.LastName.ToUpper().Contains(exclude)
                     || a.Jobseeker.Specialization.ToUpper().Contains(exclude)
                     || a.Jobseeker.WebsiteURL.ToUpper().Contains(exclude))
                {
                    advancedSearchResults.Remove(a);
                }
            }
            //check educational institute
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                JobseekerAccount a = advancedSearchResults[i];
                if (educationalInstitute != "")
                {
                    if (!a.Jobseeker.PreviousEducationalInstitute.ToUpper().Contains(educationalInstitute))
                    {
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            return advancedSearchResults;
        }
    }
}
